package cn.com.dhcc.turbo.service.registry;

  
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.dao.CrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.AbstractCrudService;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;
import cn.com.dhcc.turbo.dao.registry.AdapterBaseDao;
import cn.com.dhcc.turbo.dao.registry.AdapterInfoDao;
import cn.com.dhcc.turbo.entity.registry.AdapterBase;
import cn.com.dhcc.turbo.entity.registry.AdapterInfo;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo.SyncOperType;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo.SyncType;
import cn.com.dhcc.turbo.service.registry.sync.SynchronizeHandler;

/**
 * 
 * 
 * @日期 2015-4-20 上午10:34:48
 * @作者  高强
 */
@Service
public class AdapterBaseService extends AbstractCrudService<AdapterBase> { 
	@Autowired
	private AdapterBaseDao dao;
	@Autowired
	@Qualifier(AppIocBean.BASE_INFO_SERVICEIMPL_BEAN)
	BaseInfoService baseInfoService;
	
	@Autowired
	private AdapterInfoDao adapterInfoDao;
	
	
	@Autowired
	SynchronizeHandler synchronizeHandler;
	
//	private Logger log = Log.getLogger(LOGTYPE.TURBO);
	
	@Override
	public CrudDao<AdapterBase> getCrudDao() {
		return dao;
	}


	
	public void insertByAdapterBase(AdapterBase entity)throws ServiceException {
		//修改AdapterBaseInfo
		NodeInfo localNode=baseInfoService.getCurrentNodeInfo();
		AdapterInfo adapterInfo=baseInfoService.getAdapterInfo(localNode.getNodeCode(), entity.getAdapterCode());
		
		//获取本节点的行政区号
		String orgCode=localNode.getNodeCode().substring(0,6);//本节点行政区 取前六位
		String parentCode=baseInfoService.getNodeInfoById(localNode.getPid())==null? "":baseInfoService.getNodeInfoById(localNode.getPid()).getNodeCode().substring(0,6);//父节点行政区
		if (entity.getOrgCode().equals(orgCode) || entity.getOrgCode().equals(parentCode) || entity.getAdapterType().equals(AppConst.OBJECT_TYPE.GLOBAL)) {
			if (adapterInfo==null) {
				adapterInfo=new AdapterInfo();
				adapterInfo.setId(UUIDGenerator.getUUID());
				adapterInfo.setIslocal("1");
				adapterInfo.setNodeCode(localNode.getNodeCode());
				adapterInfo.setAdapterType(entity.getAdapterType());
				adapterInfo.setOrgCode(entity.getOrgCode());
			}
			adapterInfo.setAdapterCode(entity.getAdapterCode());
			adapterInfo.setAdapterName(entity.getAdapterName());
			try {
				adapterInfoDao.insertOrUpdate(adapterInfo);
			} catch (DaoException e) {
				throw new ServiceException("更新adapterInfo失败", e);
			}
		}
	}
	
	/**
	 * 获取最大编码
	 * */
	public long localMaxCode(String adapterType,String orgCode) throws ServiceException {
		try {
			return dao.localMaxCode(adapterType,orgCode);
		} catch (DaoException e) {
			throw new ServiceException("适配器编码失败", e);
		}
	};
	

	/**
	 * 添加并同步
	 * @param entity
	 * @throws ServiceException
	 * @throws NiceException
	 */
	public void insertAdapterBaseAndSync(AdapterBase entity) throws ServiceException, NiceException {
		super.insert(entity);
		insertByAdapterBase(entity);
		try {
			synchronizeHandler.syncToAllNodesFromLocalNode(SyncType.ADAPTER_BASE, false, entity, SyncOperType.INSERT,true);
		} catch (Exception e) {
			throw new NiceException("添加成功，但数据同步失败，请手动同步!");
		}
		
	
	}
	/**
	 * 修改并同步
	 * @param entity
	 * @throws ServiceException
	 * @throws NiceException
	 */
	public void updateAdapterBaseAndSync(AdapterBase entity) throws ServiceException, NiceException {
		super.updateIgnoreNull(entity);
		insertByAdapterBase(entity);
		try {
			AdapterBase inDbentity = get(entity.getId());
			synchronizeHandler.syncToAllNodesFromLocalNode(SyncType.ADAPTER_BASE, false, inDbentity, SyncOperType.UPDATE,true);
		} catch (Exception e) {
			throw new NiceException("添加成功，但数据同步失败，请手动同步!");
		}
	}


	public int insertOrUpdateRemoteNode(AdapterBase entity)  throws ServiceException{
		int r = super.insertOrUpdate(entity);
		insertByAdapterBase(entity);
		return r;
	}
	
	public PagerData<AdapterBase> getAdapterBaseList(Pager pager, WhereCondition condition,String orgCode)throws ServiceException{
		try {
			
			return dao.getAdapterBaseList(pager,condition,orgCode);
		} catch (Exception e) {
			throw new ServiceException("添加成功，但数据同步失败，请手动同步!");
		}
	}
}
